package cn.main.web;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by cheng on 2016/5/14.
 */
public class ConfigLoad {

    private JdbcTemplate jdbcTemplate;

    public ConfigLoad() {

    }

    public ConfigLoad(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Properties init() {
        Properties prop = new Properties();
        //获取数据库系统配置
        try {

            List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from web_config");
            for (Map<String,Object> map : list) {
                prop.put(String.valueOf(map.get("web_key")),map.get("web_value"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        //获取文件配置
        // 方法二：通过类加载目录getClassLoader()加载属性文件

        // InputStream in =
        // this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");

        // 注：Object.class.getResourceAsStream在action中调用报错，在普通java工程中可用
        // InputStream in =
        // Object.class.getResourceAsStream("/mailServer.properties");
//        try {
//            InputStream in = this.getClass().getClassLoader()
//                    .getResourceAsStream("");
//            prop.load(in);
//
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        return prop;

    }

}
